package com.lzb.musicplayer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.lzb.musicplayer.R;


public class VerticalScaleView extends View {
    // 显示方向常量
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int BOTH = 2;

    // 默认尺寸（单位：dp）
    private static final float DEFAULT_MAJOR_WIDTH_DP = 10f;
    private static final float DEFAULT_MAJOR_HEIGHT_DP = 1f;
    private static final float DEFAULT_MINOR_WIDTH_DP = 5f;
    private static final float DEFAULT_MINOR_HEIGHT_DP = 1f;
    private static final int DEFAULT_SCALE_COLOR = Color.BLACK;

    // 尺寸配置（px单位）
    private int majorTickWidth;
    private int majorTickHeight;
    private int minorTickWidth;
    private int minorTickHeight;
    private int scaleColor;

    // 显示状态
    private int scalePosition = BOTH;
    private Paint majorPaint;
    private Paint minorPaint;

    public VerticalScaleView(Context context) {
        super(context);
        init(context, null);
    }

    public VerticalScaleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 转换默认值为像素
        majorTickWidth = dpToPx(context, DEFAULT_MAJOR_WIDTH_DP);
        majorTickHeight = dpToPx(context, DEFAULT_MAJOR_HEIGHT_DP);
        minorTickWidth = dpToPx(context, DEFAULT_MINOR_WIDTH_DP);
        minorTickHeight = dpToPx(context, DEFAULT_MINOR_HEIGHT_DP);
        scaleColor = DEFAULT_SCALE_COLOR;

        // 读取自定义属性
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalScaleView);

            majorTickWidth = a.getDimensionPixelSize(
                    R.styleable.VerticalScaleView_majorTickWidth, majorTickWidth);
            majorTickHeight = a.getDimensionPixelSize(
                    R.styleable.VerticalScaleView_majorTickHeight, majorTickHeight);

            minorTickWidth = a.getDimensionPixelSize(
                    R.styleable.VerticalScaleView_minorTickWidth, minorTickWidth);
            minorTickHeight = a.getDimensionPixelSize(
                    R.styleable.VerticalScaleView_minorTickHeight, minorTickHeight);

            scalePosition = a.getInt(R.styleable.VerticalScaleView_scalePosition, BOTH);
            scaleColor = a.getColor(R.styleable.VerticalScaleView_scaleColor, scaleColor);

            a.recycle();
        }

        // 初始化画笔
        majorPaint = createPaint(majorTickHeight, scaleColor);
        minorPaint = createPaint(minorTickHeight, scaleColor);
    }

    private Paint createPaint(int strokeWidth, int color) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setStrokeWidth(strokeWidth);
        return paint;
    }

    private int dpToPx(Context context, float dp) {
        int i = (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
        Log.d("ScaleView", "Converted " + dp + "dp to " + i + "px");
        return i;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 计算最小宽度需求
        int minTickWidth = Math.max(majorTickWidth, minorTickWidth);
        int requiredWidth = minTickWidth * (scalePosition == BOTH ? 2 : 1);
        requiredWidth += getPaddingLeft() + getPaddingRight();

        // 处理宽度测量
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int finalWidth;
        switch (widthMode) {
            case MeasureSpec.EXACTLY:
                finalWidth = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                finalWidth = Math.min(requiredWidth, widthSize);
                break;
            case MeasureSpec.UNSPECIFIED:
            default:
                finalWidth = requiredWidth;
                break;
        }

        // 处理高度测量
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int finalHeight = heightMode == MeasureSpec.UNSPECIFIED ?
                getSuggestedMinimumHeight() : heightSize;

        setMeasuredDimension(finalWidth, finalHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        int contentWidth = getWidth() - getPaddingLeft() - getPaddingRight();

        final int totalTicks = 31;  // 15到-15共31个刻度
        final float interval = viewHeight / 30f;

        float baseY = getPaddingTop(); // 顶部起始位置

        for (int i = 0; i < totalTicks; i++) {
            float yPos = baseY + i * interval;
            int currentValue = 15 - i;

            boolean isMajor = currentValue % 5 == 0;
            int tickWidth = isMajor ? majorTickWidth : minorTickWidth;
            Paint currentPaint = isMajor ? majorPaint : minorPaint;

            // 绘制左侧刻度
            if (scalePosition == LEFT || scalePosition == BOTH) {
                float startX = getPaddingLeft();
                float endX = startX + tickWidth;
                canvas.drawLine(startX, yPos, endX, yPos, currentPaint);
            }

            // 绘制右侧刻度
            if (scalePosition == RIGHT || scalePosition == BOTH) {
                float endX = getWidth() - getPaddingRight();
                float startX = endX - tickWidth;
                canvas.drawLine(startX, yPos, endX, yPos, currentPaint);
            }
        }
    }

    // region 公开方法
    public void setScalePosition(int position) {
        if (position < LEFT || position > BOTH) return;
        if (scalePosition != position) {
            scalePosition = position;
            requestLayout(); // 需要重新测量
            invalidate();
        }
    }

    public void setMajorTickSize(int widthPx, int heightPx) {
        majorTickWidth = widthPx;
        majorPaint.setStrokeWidth(heightPx);
        requestLayout();
        invalidate();
    }

    public void setMinorTickSize(int widthPx, int heightPx) {
        minorTickWidth = widthPx;
        minorPaint.setStrokeWidth(heightPx);
        requestLayout();
        invalidate();
    }

    public void setScaleColor(int color) {
        scaleColor = color;
        majorPaint.setColor(color);
        minorPaint.setColor(color);
        invalidate();
    }
    // endregion
}